import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/pages/promotion/promotion_withdraw/promotion_withdraw_logic.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:get/get.dart';
import '../config/app_color.dart';

/// 通用下拉选择组件 - 基于Figma设计图实现
class BtnDropdown extends StatelessWidget {
  final List<String> items;
  final List<ModelPromotionDropdown>? customStyle;
  final List<ModelHomeDropdown>? customHomeStyle;

  // ModelHomeDropdown
  final String? value;
  final ValueChanged<String> onChanged;
  final Widget child;
  final MainAxisAlignment? mainAxisAlignment;
  final double? width;

  const BtnDropdown({
    super.key,
    required this.items,
    this.value,
    this.customHomeStyle,
    required this.onChanged,
    required this.child,
    this.mainAxisAlignment,
    this.width,
    this.customStyle,
  });

  @override
  Widget build(BuildContext context) {
    return DropdownButtonHideUnderline(
      child: DropdownButton2<String>(
        // value: value,
        value: (value ??'').isNotEmpty ? value : null,
        customButton: child,
        items: customStyle != null
            ? (customStyle ?? []).map((item) {
                return DropdownMenuItem<String>(
                  value: item.tag,
                  child: Container(
                      width: double.infinity,
                      padding: EdgeInsets.symmetric(
                          horizontal: 12.w, vertical: 14.w),
                      decoration: BoxDecoration(
                        color: item.tag == value
                            ? AppColor.color242631
                            : Colors.transparent,
                        borderRadius: BorderRadius.circular(4.w),
                      ),
                      child: Row(
                        children: [
                          Text(
                            item.title ?? "",
                            style: TextStyle(
                              fontFamily: 'PingFang SC',
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w400,
                              color: AppColor.textPrimary.withOpacity(0.94),
                            ),
                          ),
                          const Spacer(),
                          AutoImage(
                            UtilsOther().getAmountTagIcon(item.symbol),
                            width: 20.w,
                          ).marginOnly(right: 4.w),
                          Text(
                            item.balance.formatWallet(),
                            // textAlign: TextAlign.center,
                            style: TextStyle(
                              fontFamily: 'PingFang SC',
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w900,
                              color: AppColor.textPrimary.withOpacity(0.94),
                            ),
                          ),
                          8.horizontalSpace,
                          Text(
                            item.symbol ??'',
                            // textAlign: TextAlign.center,
                            style: TextStyle(
                              fontFamily: 'PingFang SC',
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w400,
                              color: AppColor.textPrimary.withOpacity(0.7),
                            ),
                          ),
                          // AutoImage(
                          //   Assets.promotionCommissionCaretDown,
                          //   width: 16.w,
                          // )
                        ],
                      )),
                );
              }).toList()
            : customHomeStyle != null
                ? (customHomeStyle ?? []).map((item) {
                    return DropdownMenuItem<String>(
                      value: item.symbol,
                      child: Container(
                          width: double.infinity,
                          padding: EdgeInsets.symmetric(
                              horizontal: 12.w, vertical: 14.w),
                          decoration: BoxDecoration(
                            color: item.symbol == value
                                ? AppColor.color242631
                                : Colors.transparent,
                            borderRadius: BorderRadius.circular(4.w),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              AutoImage(
                                UtilsOther().getAmountTagIcon(item.symbol),
                                width: 20.w,
                              ).marginOnly(right: 4.w),
                              Text(
                                item.balance.formatWallet(),
                                // textAlign: TextAlign.center,
                                style: TextStyle(
                                  fontFamily: 'PingFang SC',
                                  fontSize: 14.sp,
                                  fontWeight: FontWeight.w900,
                                  color: AppColor.textPrimary.withOpacity(0.94),
                                ),
                              ),
                            ],
                          )),
                    );
                  }).toList()
                : items.map((item) {
                    // GetBuilder
                    String? icon = item == "TRX"
                        ? Assets.commonTrxTag
                        : item == "USDT"
                            ? Assets.commonUsdtTag
                            : null;
                    return DropdownMenuItem<String>(
                      value: item,
                      child: Container(
                          width: double.infinity,
                          padding: EdgeInsets.symmetric(
                              horizontal: 12.w, vertical: 12.w),
                          decoration: BoxDecoration(
                            color: item == value
                                ? AppColor.color242631
                                : Colors.transparent,
                            borderRadius: BorderRadius.circular(4.w),
                          ),
                          child: Row(
                            mainAxisAlignment:
                                mainAxisAlignment ?? MainAxisAlignment.center,
                            children: [
                              // Visibility(child: AutoImage(name)),
                              if (icon != null)
                                AutoImage(
                                  icon,
                                  width: 20.w,
                                ).marginOnly(right: 8.w),
                              Text(
                                item,
                                // textAlign: TextAlign.center,
                                style: TextStyle(
                                  fontFamily: 'PingFang SC',
                                  fontSize: 14.sp,
                                  fontWeight: FontWeight.w400,
                                  color: item == value
                                      ? AppColor.textPrimary.withOpacity(0.94)
                                      : AppColor.textPrimary.withOpacity(0.7),
                                ),
                              ),
                            ],
                          )),
                    );
                  }).toList(),
        onChanged: (newValue) {
          if (newValue != null) onChanged(newValue);
        },

        buttonStyleData: const ButtonStyleData(
          height: 0,
          width: 0,
          padding: EdgeInsets.zero,
          decoration: BoxDecoration(color: Colors.transparent),
        ),
        dropdownStyleData: DropdownStyleData(
          maxHeight: 250.w,
          width: width ?? 170.w,
          decoration: BoxDecoration(
            color: AppColor.dropdownBackground,
            borderRadius: BorderRadius.circular(8.w),
          ),
          padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 4.w),
          offset: const Offset(0, -4),
          scrollbarTheme: ScrollbarThemeData(
            radius: Radius.circular(2.w),
            thickness: WidgetStateProperty.all(4.w),
            thumbVisibility: WidgetStateProperty.all(true),
            thumbColor:
                WidgetStateProperty.all(AppColor.textPrimary.withOpacity(0.15)),
          ),
        ),
        menuItemStyleData: MenuItemStyleData(
          height: 52.w,
          padding: EdgeInsets.zero,
        ),
        iconStyleData: const IconStyleData(
          icon: SizedBox.shrink(),
        ),
      ),
    );
  }
}
